﻿using GodSharp.Opc.Da;
using PPSMS.Controllers;
using PPSMS_BLL;
using PPSMS_Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PPSMS.CommonForm
{
    public partial class AddInverterForm : Form
    {
        private InverterDevices inverterDevices = new();

        private InverterDevices_BLL _BLL = new InverterDevices_BLL();

        private StationStatus_BLL status_BLL = new StationStatus_BLL();

        private List<long> Ids = new();

        public event Action OnRefres;

        public static event Action<InverterControl> OnInverterControlRefresh;

        public long InverterID
        {
            get => long.Parse(input_InverterID.Text);
            set => input_InverterID.Text = value.ToString();
        }

        /// <summary>
        /// 制造商
        /// </summary>
        public string Manufacturer
        {
            get => input_manufacturer.Text;
            set => input_manufacturer.Text = value;
        }

        /// <summary>
        /// 逆变器型号
        /// </summary>
        public string Model
        {
            get => input_model.Text;
            set => input_model.Text = value;
        }

        /// <summary>
        /// 额定功率
        /// </summary>
        public string RatedPower
        {
            get => INcommunicationPower.Text;
            set => INcommunicationPower.Text = value;
        }

        /// <summary>
        /// 组件峰值功率
        /// </summary>
        public string Module_Peak_Power
        {
            get => INpeakPower.Text;
            set => INpeakPower.Text = value;

        }

        public AddInverterForm()
        {
            InitializeComponent();

        }

        public AddInverterForm(InverterDevices control) : this()
        {
            inverterDevices = control;
        }

        private void AddInverterForm_Load(object sender, EventArgs e)
        {
            var InverterIDs = _BLL.GetInverterDeviceIds();
            foreach (var ID in InverterIDs)
            {
                Ids.Add(ID);
            }
            if (inverterDevices.Manufacturer != null)
            {
                InverterID = inverterDevices.InverterID;
                Manufacturer = inverterDevices.Manufacturer;
                Model = inverterDevices.Model;
                RatedPower = inverterDevices.RatedPower;
                Module_Peak_Power = inverterDevices.Module_Peak_Power;
                button1.Text = "修改";
                button1.Type = AntdUI.TTypeMini.Success;
            }
            else
            {
                button1.Text = "新增";
                button1.Type = AntdUI.TTypeMini.Success;

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Model == "" && Manufacturer == "" && RatedPower == "0" && Module_Peak_Power == "0")
            {
                MessageBox.Show("请正确填写!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                InputEmpty();
            }
            else
            {
                inverterDevices.InverterID = InverterID;
                inverterDevices.Manufacturer = Manufacturer;
                inverterDevices.Model = Model;
                inverterDevices.RatedPower = RatedPower;
                inverterDevices.Module_Peak_Power = Module_Peak_Power;
            }
            if (button1.Text == "新增")
            {
                if (!Ids.Equals(InverterID))
                {
                    _BLL.Add(inverterDevices);
                    status_BLL.UpdateCount(true);
                    OnInverterControlRefresh?.Invoke(new InverterControl(inverterDevices));
                    OnRefres?.Invoke();
                    this.Close();
                }
                else
                {
                    InputEmpty();
                }
                
            }
            else
            {
                _BLL.UpdateById(inverterDevices, inverterDevices.InverterID);
                OnInverterControlRefresh?.Invoke(new InverterControl(inverterDevices));
                OnRefres?.Invoke();
                this.Close();
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认取消操作？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
            {
                this.Close();
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            InputEmpty();
        }

        /// <summary>
        /// 清空输入框
        /// </summary>
        private void InputEmpty()
        {
            long id = 0;
            foreach (var i in Ids)
            {
                if (i > id)
                {
                    id = i;
                }
            }
            InverterID = id + 1;
            Manufacturer = "";
            Model = "";
            RatedPower = "";
            Module_Peak_Power = "";
            AntdUI.Message.success(this, $"推荐编号:{InverterID}");
        }
    }
}
